﻿using System;

namespace Achievements
{
    public sealed class IncrementableAchievement : Achievement, IIncrementableAchievement
    {
        public const string CurrentAmountPropertyString = "CurrentAmount";
        public const string RemainingAmountPropertyString = "RemainingAmount";

        #region Implementation of IIncrementableAchievement

        private int _currentAmount;
        private int? _unlockAmount;

        /// <summary>
        /// Increments this instance by 1.
        /// </summary>
        public void Increment()
        {
            if (IsUnlocked) return;

            CurrentAmount++;
            if (RemainingAmount == 0)
                UnlockAchievement();
        }

        /// <summary>
        /// Gets the unlock amount.
        /// </summary>
        public int UnlockAmount
        {
            get { return _unlockAmount.GetValueOrDefault(); }
            set
            {
                if (_unlockAmount <= 0 || _unlockAmount.HasValue)
                {
                    throw new ArgumentException("UnlockAmount");
                }
                _unlockAmount = value;
            }
        }

        /// <summary>
        /// Gets the current amount.
        /// </summary>
        public int CurrentAmount
        {
            get { return _currentAmount; }
            set
            {
                if (_currentAmount != value)
                {
                    _currentAmount = value;
                    RaisePropertyChanged(CurrentAmountPropertyString);
                    RaisePropertyChanged(RemainingAmountPropertyString);
                }
            }
        }

        /// <summary>
        /// Gets the remaining amount.
        /// </summary>
        public int RemainingAmount
        {
            get
            {
                if (IsUnlocked) return 0;
                return Math.Max(0, UnlockAmount - CurrentAmount);
            }
        }

        #endregion
    }
}